
#include "appXssl.h"

unsigned char g_ucDebug = DBG_ON;

static void xs_firmware_init(void)
{ 
  systick_init();

  uart1_init( 115200, 
              USART_WordLength_8b, 
              USART_StopBits_1, 
              USART_Parity_No, 
              USART_HardwareFlowControl_None);
  print_log(D_INFO, ("Xssl Version %s\n", XSSL_VER));
	print_log(D_INFO, ("Xssl Compile :%s-%s\n", __TIME__, __DATE__));

  led_init();
  buzzer_init();
	
  tim3_dev_init();
	tim4_dev_init();

	init_TcpNet();
  
  get_user_cfg();
  
  traffic_statistics_read();
  
  sys_stat_led_on();
  
  gprs_signal_led_on();
	//iwdg_init();
}

void main_task(void)
{
  int iRet = S_OK;
  unsigned char ucReset = SWITCH_OFF;
	
  iRet = xc_proc_init();
  while(0 != iRet){
    buzzer_ring(1);
  }
  print_log(D_INFO, ("xssl main task start\n"));
  buzzer_ring(2);
  while(1) {
    //iwdg_reload();
		iRet = gprs_main_proc();

    if(G_PPP_RUNNING == iRet){
      traffic_statistics_write();
      iRet = xc_main_proc(ucReset);
			if(P2P_FORWARD == iRet){
				iRet = proxy_comm_proc();
        if(XS_XML_LOGOUT == iRet){
          ucReset = SWITCH_ON;
          continue;
        }
			}else if(P2P_OK == iRet){
				p2p_comm_proc();
			}
      ucReset = SWITCH_OFF;
    }else if(G_PPP_DROP == iRet && SWITCH_OFF == ucReset){
      ucReset = SWITCH_ON;
    }
  }    
}

int main(void)
{
  xs_firmware_init();
  main_task();
  return 0;
}

